package com.ruoyi.project.platform.agent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.Test;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Base64;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.ruoyi.common.constant.Constants.*;

@Slf4j
@Component
public class GiteeAgent {
    @Data
    public static class FileDesc {
        private String type;   // "file"
        private String size;   // 0
        private String name;   // "parameters.json"
        private String path;   // "src/com.yunkun/depth/parameters.json"
        private String sha;   // "fa894620c51240f9e23a40d3bb13282eef87fbf9"
        private String url;   // "https://gitee.com/api/v5/repos/huasir/algorithm/contents/src/com.yunkun/depth/parameters.json"
        @JsonProperty("html_url")
        private String htmlUrl;   // "https://gitee.com/huasir/algorithm/blob/depth-release-0.0.1/src/com.yunkun/depth/parameters.json"
        @JsonProperty("download_url")
        private String downloadUrl;   // "https://gitee.com/huasir/algorithm/raw/depth-release-0.0.1/src/com.yunkun/depth/parameters.json"
    }

    @Data
    public static class FileBlob {
        private String sha;  // "fa894620c51240f9e23a40d3bb13282eef87fbf9"
        private String size;  // 530
        private String url;  // "http://gitee.com/api/v5/repos/huasir/algorithm/git/blobs/fa894620c51240f9e23a40d3bb13282eef87fbf9"
        private String content;  // "Ww0KIFsiaW50ZXZhbCIsIuWIt+aWsOWRqOacnyIsIjMiXSwNCiBbInByaWNlX3JhdGUiLCLmgLvku7fmoLzmjILljZXojIPlm7QiLCIwLjUiXSwNCiBbIm1heF9mbHlfb3JkZXJfcXR5Iiwi5Y2V56yU5pyA5aSn5oyC5Y2V5pWw6YePIiwiNTAwMCJdLA0KIFsiaW5uZXJfcHJpY2VfcmF0ZSIsIuWGheWMuuS7t+agvOiMg+WbtCIsIjAuMyJdLA0KIFsiaW5uZXJfZGVwdGhfbnVtIiwi5YaF5Yy65oyC5Y2V5rex5bqmIiwiMjAiXSwNCiBbInRlbXBfaW5jcmVhc2VfaW5uZXJfcXR5Iiwi5YaF5Yy65Li05pe25aKe5Yqg5pWw6YePIiwiMCJdLA0KIFsib3V0dGVyX2RlcHRoX251bSIsIuWkluWMuuaMguWNlea3seW6piIsIjUwIl0sDQogWyJ0ZW1wX2luY3JlYXNlX291dHRlcl9xdHkiLCLlpJbljLrpop3lpJblop7liqDmlbDph48iLCIwIl0sDQogWyJyZWZyZXNoX21vZGUiLCLliLfmlrDmt7HluqbmqKHlvI8iLCJpbm5lcl9vbmx5IiwiZW51bTppbm5lcl9vbmx5OuWAvOWIt+aWsOWGheWMujthbGw65YaF5aSW5Yy65LiA6LW35Yi3Il0NCl0="
        private String encoding;  // "base64"
    }


    public List<List<String>> syncParameterWithTag(String tag) {
//        String tag = "depth-release-0.0.1";
        List<FileDesc> fileDescs = getContentByTag(tag);
        List<List<String>> result = null;
        if (!fileDescs.isEmpty()) {
            for (FileDesc m : fileDescs) {
                if (m.getName().equals(PARAMETER_F_NAME)) {
                    m.getHtmlUrl();
                    String content = getBlob(m.getSha()).getContent();
                    String jsonContent = new String(Base64.decode(content));
                    log.info("get tag {} result param {}", tag, jsonContent);
                    if (!StringUtils.isEmpty(jsonContent)) {
                        result = JSON.parseObject(jsonContent, new TypeReference<List<List<String>>>() {
                        }.getType());
                    }
                }
            }
        }
        return result;
    }

    private List<FileDesc> getContentByTag(String tag) {
        String folder = tag.split("-")[0];
        String url = GITEE_CONTENT_BASE + "/src/com.yunkun/" + folder;
        String param = "access_token=" + GITEE_ACCESS_TOKEN + "&ref=" + tag;
        String result = HttpUtils.sendGet(url, param);
        return JSONObject.parseObject(result, new TypeReference<List<FileDesc>>() {
        }.getType());
    }

    private FileBlob getBlob(String hash) {
        String url = GITEE_BLOB_BASE + "/" + hash;
        String param = "access_token=" + GITEE_ACCESS_TOKEN;
        String result = HttpUtils.sendGet(url, param);
        return JSONObject.parseObject(result, new TypeReference<FileBlob>() {
        }.getType());
    }
}
